
<template>
  <span class="ecp-dialog-btn">
    <el-button :disabled="disabled" type="primary" size="mini" @click="visabled = true">添加</el-button>
    <el-dialog title="添加分类" :visible.sync="visabled" width="320px" top="212px" center>
      <el-form :model="dialogForm" ref="dialogForm" label-width="90px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="name" :rules="[
            { required: true, message: '请输入分类名称'}
          ]">
          <el-input type="text" v-model="dialogForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="orderIndex" :rules="[
            { required: true, message: '请输入排序'},
            { type: 'number', message: '排序必须为数字值'}
          ]">
          <el-input type="text" v-model.number="dialogForm.orderIndex" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </span>
</template>

<script>
import { addPurchaseCategoryParent } from "~/api/";
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visabled: false,
      dialogForm: {
        name: '',
        orderIndex: 1
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs['dialogForm'].validate(async (valid) => {
        if (!valid) return
        const res = await addPurchaseCategoryParent(this, this.dialogForm.name, this.dialogForm.orderIndex)
        // 成功处理
        if (res.code == '0') {
          this.$message.success('添加成功')
          this.$emit('on-success', this.dialogForm)
          this.visabled = false
          return
        }
        // 失败处理
        this.$msgbox({
          title: '提示',
          message: res.message,
          type: 'error'
        })
      })
    },
    resetForm() {
      this.$refs['dialogForm'].resetFields()
      this.visabled = false
    }
  }
}
</script>

<style lang="scss" scoped>
.ecp-dialog-btn {
  & /deep/ {
    .el-dialog--center .el-dialog__body {
      padding: 15px;
    }
  }
}
</style>
